<?php
/**
 * @param string $url
 * @param string $param
 * @return - http response body if succeeds, else false.
 */
include_once"access_token.php";

function request($url = '', $param = '')
{
	if (empty($url) || empty($param)) {
		return false;
    }

    $postUrl = $url;
    $curlPost = $param;
    // 初始化curl
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $postUrl);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    // post提交方式
    curl_setopt($curl, CURLOPT_POST, 1);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $curlPost);
    // 运行curl
    $data = curl_exec($curl);
    curl_close($curl);

    return $data;
}

function is_fire()
{
	$token = t_token();
	$url = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general?access_token=' . $token;
	$img = file_get_contents('/var/www/html/fire/images/my.bmp');
	$img = base64_encode($img);
	$bodys = array(
		'image' => $img
	);
	$res = request($url, $bodys);
	
	$jsonDecoded = json_decode($res,true);
	if(!$jsonDecoded)
	{
		echo "解析失败！\n";
		//return false;
	}

	$number = 0;
	foreach($jsonDecoded['result'] as $sub_array)
	{

		$photoId = $sub_array['keyword'];
		if(true == strpos($photoId,"火"))
		{

			$number = $number + 1;
		}
	}
	if($number >= 1)
	{
		printf("%s :flame detected！\n",date("Y-m-d H:i:s"));
		return true;
	}
	else
	{
		printf("%s :no Flame was detected！\n",date("Y-m-d H:i:s"));
		return true;
	}

}

?>
